package com.jinlejia.bbs.logic.mine;

import android.content.Context;
import android.text.TextUtils;

import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.TagBean;
import com.jinlejia.bbs.http.entity.MineEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCall;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
/**
 * @author 王进 on 2019/3/28.
 */

public class MyInfoLogic {
    private final Context context;
    private final LogicCallBack callBack;

    public MyInfoLogic(Context context,LogicCallBack callBack){
        this.context = context;
        this.callBack = callBack;
    }

    public void execute(){
        httpCall(BBSConfig.getUser)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult(null);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        MineEntity entity = parse.parse(MineEntity.class);
                        BBSUserBean user = new BBSUserBean();
                        //PIN
                        user.setUserPin(entity.getPin());
                        //头像
                        user.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl()));
                        //姓名
                        user.setUserName(entity.getUserName());
                        //性别
                        int userSex = entity.getUserSex();
                        if (userSex==0){
                            user.setGender("女");
                        }else {
                            user.setGender("男");
                        }
                        //生日
                        String res = entity.getBirthday();
                        if (TextUtils.isEmpty(res)){
                            user.setBirthday(res);
                        }else {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
                            String format = sdf.format(new Date(Long.valueOf(res)));
                            user.setBirthday(format);
                        }


                        //所属店门
                        user.setOfShop(entity.getUserDeptName());
                        //职位
                        user.setUserDuty(entity.getDuty());

                        //标签
                        ArrayList<TagBean> tags = new ArrayList<>();
                        String entityTag = entity.getTag();
                        if (!TextUtils.isEmpty(entityTag)){
                            String[] split = entityTag.split(",");
                            for (String tag:split) {
                                TagBean bean = new TagBean();
                                bean.setValueName(tag);
                                bean.setValue(tag);
                                tags.add(bean);
                            }
                        }
                        user.setTags(tags);
                        callBack.onResult(user);
                    }


                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(null);
                    }
                }));
    }

    public interface LogicCallBack{
        void onResult(BBSUserBean user);
    }
}
